package com.xinggq.user.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinggq.common.CommonResult;
import com.xinggq.common.page.PageCondition;
import com.xinggq.common.utils.Validate;
import com.xinggq.user.service.dto.user.UserCreateRequest;
import com.xinggq.user.service.dto.user.UserUpdateRequest;
import com.xinggq.user.service.dto.user.UserPageRequest;
import com.xinggq.user.service.dto.user.UserVO;
import com.xinggq.user.service.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 用户管理控制器
 *
 * @author xinggq
 * @since 2025/10/30
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户管理相关接口")
@Slf4j
@AllArgsConstructor
public class UserController {

  private final UserInfoService userInfoService;

  /**
   * 创建用户
   */
  @PostMapping("/create")
  @Operation(summary = "创建用户", description = "创建新用户")
  public CommonResult<UserVO> createUser(@Valid @RequestBody UserCreateRequest request) {
    UserVO createdUser = userInfoService.createUser(request);
    return CommonResult.ok(createdUser);
  }

  /**
   * 更新用户
   */
  @PostMapping("/update")
  @Operation(summary = "更新用户", description = "更新用户的基本信息（用户名、邮箱、手机号、真实姓名、昵称、性别、生日）")
  public CommonResult<UserVO> updateUser(@Valid @RequestBody UserUpdateRequest request) {
    UserVO updatedUser = userInfoService.updateUser(request);
    return CommonResult.ok(updatedUser);
  }

  /**
   * 获取用户详细信息
   */
  @GetMapping("/userinfo")
  @Operation(summary = "获取用户详细信息", description = "根据用户ID获取用户详细信息")
  public CommonResult<UserVO> getUserDetail(
      @Parameter(description = "用户ID", required = true)
      @RequestParam(value = "userId")
      Long userId) {
    UserVO userDetail = userInfoService.getUserDetail(userId);
    return CommonResult.ok(userDetail);
  }

  /**
   * 分页查询用户
   */
  @PostMapping("/page")
  @Operation(summary = "分页查询用户", description = "根据条件分页查询用户列表")
  public CommonResult<IPage<UserVO>> pageQuery(
      @Valid @RequestBody PageCondition<UserPageRequest> pageCondition) {
    IPage<UserVO> userPage = userInfoService.pageQuery(pageCondition);
    return CommonResult.ok(userPage);
  }

  /**
   * 启用用户
   */
  @GetMapping("/enable")
  @Operation(summary = "启用用户", description = "启用指定用户")
  public CommonResult<Boolean> enableUser(@RequestParam("userId") Long userId) {
    boolean result = userInfoService.enableUser(userId);
    return CommonResult.ok(result);
  }

  /**
   * 禁用用户
   */
  @GetMapping("/disable")
  @Operation(summary = "禁用用户", description = "禁用指定用户")
  public CommonResult<Boolean> disableUser(@RequestParam("userId") Long userId) {
    boolean result = userInfoService.disableUser(userId);
    return CommonResult.ok(result);
  }

  /**
   * 禁用用户
   */
  @GetMapping("/del")
  @Operation(summary = "删除用户", description = "删除指定用户")
  public CommonResult<Boolean> delUser(@RequestParam("userId") Long userId) {
    boolean result = userInfoService.delUser(userId);
    return CommonResult.ok(result);
  }

  /**
   * 禁用用户
   */
  @GetMapping("/check-username")
  @Operation(summary = "检查用户名是否存在", description = "检查用户名是否存在")
  public CommonResult<Boolean> checkUsername(
      @RequestParam(value = "excludeId", required = false) Long excludeUserId,
      @RequestParam("userName") String userName) {

    Validate.strMatch(userName, "^[a-zA-Z0-9_]+$", "用户编号只能包含字母、数字和下划线");
    Validate.strNotBlank(userName, "用户编号不能为空");
    boolean exist = userInfoService.existByUserName(userName, excludeUserId);
    return CommonResult.ok(exist);
  }
}